<template>
    <div class="resume-job-experience">

      <div class="job-experience" v-if="!editJobExperience">
        <h2>工作经验<span data-html2canvas-ignore="true" @click="editOpen('editJobExperience')"><i class="el-icon-circle-plus-outline"></i>添加</span></h2>
        <div class="content" v-if="jobExperienceList.length === 0">
            <el-empty :image-size="200"></el-empty>
        </div>
        <div class="content" v-else>

          <div class="job-experience-wrapper" v-for="jobExperience in jobExperienceList" :key="'job-experience'+jobExperience.id">
            <el-button type="text" icon="el-icon-edit" class="edit"
                       @click="toggleEdit('editJobExperience',jobExperience, jobExperienceForm)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete-solid" class="remove"
                       @click="remove(jobExperience)">删除</el-button>
            <img v-if="$store.state.onlyReadResume" class="icon"
                 :src="require('@/image/illustration/el-icon-company.png')"
                 style="width: 45px; height: 45px; float: left" />
            <SymbolIcon v-else icon-class="el-icon-company" />
            <div class="job-info">
              <h3>
                <span>{{ jobExperience.companyName }}</span>
                <span>担任职务：{{ jobExperience.duty}}</span>
                <span>
                    {{ jobExperience.startTime.split("-").slice(0,2).join(".") }}
                    &nbsp;-&nbsp;
                    {{ jobExperience.endTime.split("-").slice(0,2).join(".") }}
                </span>
              </h3>
              <span>工作内容：{{ jobExperience.industryName}}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="edit-job-experience editDialog" v-else>
        <div class="edit-title">编辑工作经验</div>
        <div class="edit-content">
          <el-form :model="jobExperienceForm" :rules="jobExperienceRules" ref="jobExperienceForm" label-width="100px">
            <el-form-item label="公司名称" prop="company_name">
              <el-input v-model="jobExperienceForm.companyName"></el-input>
            </el-form-item>
            <el-form-item label="职位名称" prop="duty">
              <el-input v-model="jobExperienceForm.duty"></el-input>
            </el-form-item>
            <el-form-item label="在职时间" required class="date-form-item">
              <el-form-item prop="start_time" label-width="0" class="start-date-form-item">
                <el-date-picker
                      v-model="jobExperienceForm.startTime"
                      type="month"
                      placeholder="入职时间"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd"
                      :picker-options="{
                          disabledDate(time) {
                            return time.getTime() > Date.now();
                      }}">
                </el-date-picker>
              </el-form-item>
              <el-form-item prop="end_time" label-width="0" class="end-date-form-item">
                <el-date-picker
                      v-model="jobExperienceForm.endTime"
                      type="month"
                      placeholder="离职时间"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd"
                      :picker-options="{
                          disabledDate(time) {
                            return time.getTime() > Date.now();
                      }}">
                </el-date-picker>
              </el-form-item>
            </el-form-item>
            <el-form-item label="工作内容" prop="job_description" class="editor-form-item">
              <Editor placeholder="请描述工作内容"
                      :content.sync="jobExperienceForm.industryName"
                      :menus="['list']" :showFullScreen="false"/>
            </el-form-item>
            <el-form-item class="edit-btn">
              <el-button type="primary" @click="submitForm('jobExperienceForm','editJobExperience')">保存</el-button>
              <el-button type="text" @click="resetForm('editJobExperience')">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
</template>

<script>
    import SymbolIcon from "@/components/SymbolIcon.vue";
    import Editor from "@/components/Editor.vue"
    export default {
        name: "ResumeJobExperience",
        components: { SymbolIcon, Editor},
        props: {
            resume_id: {
                type: String,
                required: true
            }
        },
        data() {
            return {
                // jobExperienceList: [],
              user:'',
                jobExperienceList: [
                ],
              //修改表单
                jobExperienceForm: {
                id:'',
                companyName: '',
                duty:'',
                startTime:'',
                endTime: '',
                industryName:''

                },
                jobExperienceRules: {
                  companyName: [
                        { required: true, message: "请输入公司名称", trigger: 'blur'},
                    ],
                    duty: [
                        { required: true, message: "请输入职位名称", trigger: 'blur'},
                    ],
                  industryName: [
                        { required: true, message: "请描述工作内容", trigger: 'blur'},
                    ],
                  startTime: [
                        { required: true, message: "请选择入职时间", trigger: 'blur'},
                    ],
                  endTime: [
                        { required: true, message: "请选择离职时间", trigger: 'blur'},
                    ],
                },
                //工作经验 编辑框
                editJobExperience: false,
            }
        },
        created() {
            this.initData();
          this.user=JSON.parse(window.localStorage.getItem("login_id"));
          this.jobExperienceList=this.user.jobExperienceList;
          this.listJob();

        },
        methods: {
            async initData() {

                // 获取完数据告知父组件已更新完毕,用于预览简历的异步通知
                this.$emit("updatePart:resume", 1)
            },
            // 切换为编辑框
            editOpen(editDialog){
                this[editDialog] = !this[editDialog];
            },
            // 切换为编辑框时 如果是编辑按钮，传递表单所需属性值
            toggleEdit(editDialog, defaultForm, editForm){
                if (defaultForm) {
                    for (const [defaultKey, defaultValue] of Object.entries(defaultForm)) {
                        for (const editKey of Object.keys(editForm)) {
                            if (editKey === defaultKey){
                                editForm[editKey] = defaultValue;
                            }
                        }
                    }
                }

                this.editOpen(editDialog);
            },
            // 过滤文本
            filterText(data) {
                if(data)
                  return data.split("\n");
                return "";
            },
            // 工作经验删除
            async remove(jobExperience) {
                const res = await this.$axios.request({
                    url: `/web/user/delete`,
                    method: "post",
                    data: jobExperience
                })
              if(res.data.msg === '删除成功') {

                this.listJob();
              }
            },
          listJob(){
            this.$axios.request({
              url: `/web/user/listjob?id=`+this.user.id,
              method: "get",
            }).then((response)=>{
              this.jobExperienceList = response.data.listjob;
            })
          },
            // 提交表单
            submitForm(formName, editDialog) {
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        const res = await this.$axios.request({
                            url: `/web/user/updatajob?userinfoId=`+this.user.id,
                            method: "post",
                            data: this.jobExperienceForm

                        });
                        if(res.data.msg === '修改成功'){
                          this.listJob();
                        }
                        this.$message.success("保存成功");
                        this.resetForm(editDialog);
                    } else {
                        this.$message.error("保存失败");
                        return false;
                    }
                });
            },
            // 重置表单
            resetForm(editDialog) {
                for (const key of Object.keys(this.jobExperienceForm)) {
                    if(key !== "resume_id") { 
                        this.jobExperienceForm[key] = "";
                    }
                }
                this.editOpen(editDialog);
            }
        },
    }
</script>

<style lang="less" scoped>
    .el-link-active{
        transition: .3s;
        &:hover{
            color: @mainColor;
        }
        &:active{
            color: @activeColor;
        }
    }

    @mainColor: #5dd5c8;
    @activeColor: #00c2b3;
    @background: #fff;
    @editBackground: #f8f9fc;
    @fontColor: #414a60;
    @salaryColor: #fb670f;
    @promptColor: #8d92a1;
    @borderColor: #dcdfe6;

    h2{
        font-size: 20px;
        padding-left: 20px;
        position: relative;
        &::before{
            content: "";
            display: block;
            width: 2px;
            height: 16px;
            position: absolute;
            border-radius: 3px;
            background: @activeColor;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
        }
        span{
            font-size: 15px;
            font-weight: normal;
            cursor: pointer;
            color: @activeColor;
          .el-link-active();
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            i{
                margin-right: 5px;
            }
        }
    }
    .content{
        margin-top: 30px;
        .job-experience-wrapper{
            padding: 10px;
            position: relative;
            &:hover{
                box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.08);
                h3 span:last-of-type{
                    display: none;
                }
                .edit,.remove{
                    display: block;
                }
            }
            .edit,.remove{
                display: none;
                position: absolute;
                right: 10px;
                top: 10px;
                z-index: 1;
                &.edit{
                    right: 80px;
                }
            }
            .icon{
                font-size: 45px;
                float: left;
                & + div{
                    margin-left: 70px;
                }
            }
        }
    }
    .edit-title{
        font-size: 16px;
        font-weight: bold;
        color: @activeColor;
    }
    .edit-content{
        margin-top: 30px;
        .el-form-item{
            width: 50%;
            &:not(.edit-btn){
                display: inline-block;
            }
            &.editor-form-item{
                width: 100%;
            }
            &.date-form-item{
                width: 50%;
                .el-date-editor.el-input, .el-date-editor.el-input__inner{
                    width: 100%;
                }
                .start-date-form-item /deep/ .el-form-item__content{
                    margin-right: 10px !important;
                }
                .end-date-form-item /deep/ .el-form-item__content{
                    margin-left: 10px !important;
                }
            }
        }
    }
    .editDialog{
        padding: 30px 40px;
        background: @editBackground;
    }
    
    .resume-job-experience{
        font-size: 16px;
        .job-experience{
            .job-info{
                h3{
                    position: relative;
                    span:nth-child(1){
                        display: block;
                        font-size: 18px;
                        font-weight: bold;
                    }
                    span:nth-child(2){
                        display: block;
                        line-height: 32px;
                        color: @promptColor;
                        font-size: 16px;
                        font-weight: normal;
                    }
                    span:nth-child(3){
                        font-size: 16px;
                        font-weight: normal;
                        position: absolute;
                        right: 0;
                        top: 0;
                    }
                }
                p{
                    font-size: 14px;
                    line-height: 32px;
                    &:first-of-type{
                        margin-top: 15px;
                    }
                }
            }
        }
    }
</style>